﻿/**
 * Jean-François Desgagné 2009 Portfolio
 * Copyright (c) 2009 Jean-François Desgagné <jfesgagne@gmail.com>
 * 
 * This work is licensed under a Creative Commons Attribution 3.0 United States License.
 * Some Rights Reserved.
 */
package com.jfdesgagne.jfdesgagne09.modules.showcase.model {
	import com.jfdesgagne.jfdesgagne09.modules.showcase.ApplicationFacade;
	import com.jfdesgagne.jfdesgagne09.modules.showcase.model.ProjectsProxy;
	import com.jfdesgagne.jfdesgagne09.shell.model.ConfigProxy;
	import flash.net.URLRequest;
	import com.jfdesgagne.jfdesgagne09.common.PipeAwareModuleConstants;
	
	import org.puremvc.as3.multicore.interfaces.IProxy;
    import org.puremvc.as3.multicore.patterns.proxy.Proxy;
	
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.events.ProgressEvent;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	
    public class SlideshowProxy extends Proxy implements IProxy {
        public static const NAME:String = "SlideshowProxy";
		private var _imageLoader:Loader;
		private var _currentImageToLoad:Number = 0;
		private var _projectId:Number = 0;
		private var _images:Array;
		
        public function SlideshowProxy($projectId) {
        	super(NAME, new Object());
			this._projectId = $projectId;
		}
		
		public static function getInstance():SlideshowProxy {
			return ApplicationFacade.getInstance(ApplicationFacade.NAME).retrieveProxy(SlideshowProxy.NAME) as SlideshowProxy;
		}
		
		override public function onRegister():void {
			this._images = new Array();
			this._imageLoader = new Loader();
			this._imageLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, this._loaderProgressHandler);
			this._imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, this._loaderCompleteHandler);
			this._imageLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, this._loaderErrorHandler);
			
			this._loadNextImages();
		}
		
		private function _loadNextImages():void {
			var $projects:Array = ProjectsProxy.getInstance().projects;
			var $imagePath = ConfigProxy.getInstance().parameters.http_address + ConfigProxy.getInstance().config.path.showcase + $projects[this._projectId].images[this._currentImageToLoad];
			trace($imagePath);
			this._imageLoader.load(new URLRequest($imagePath));
		}
		
		private function _loaderProgressHandler($event:ProgressEvent):void {
			trace("_loaderProgressHandler");
			var $percent:Number = 30;
			this.sendNotification(PipeAwareModuleConstants.PROGRESS_LOADING, $percent);
			this.facade.getShellInstance().sendNotification(this.facade.getShell().IMAGELOAD_PROGRESS, $percent);
		}
		
		private function _loaderCompleteHandler($event:Event):void {
			this._currentImageToLoad++;
			this._images.push(this._imageLoader.content.loaderInfo as Bitmap);
			var $projects:Array = ProjectsProxy.getInstance().projects;
			if (this._currentImageToLoad < $projects[this._projectId].images.length) {
				this._loadNextImages();
			} else {
				this.sendNotification(ApplicationFacade.IMAGESLOAD_COMPLETE, this._images);
			}
		}
		
		private function _loaderErrorHandler($event:IOErrorEvent):void {
			trace("error loading file:" + $event.text);
		}
		
    }
}